<?
/*
    [TITLE] => Новости
    [SHOW_ALWAYS] => Y
    [COUNT] => 53
    [NAV_VAR] => PAGEN_1
    [PAGE_COUNT] => 5
    [PAGE_NUMBER] => 1
    [SHOW_COUNT] => 4

    $arParams['']
*/

$arResult['TITLE'] = trim($arParams['TITLE']);
$arResult['NAV_VAR'] = trim($arParams['NAV_VAR']);
$arResult['SHOW_ALWAYS'] = $arParams['SHOW_ALWAYS'] == 'Y';
$arResult['COUNT'] = (int) $arParams['COUNT'];
$arResult['PAGE_COUNT'] = (int) $arParams['PAGE_COUNT'];
$arResult['PAGE_NUMBER'] = (int) $arParams['PAGE_NUMBER'];
$arResult['SHOW_COUNT'] = (int) $arParams['SHOW_COUNT'];



$arResult['PAGE_NUMBER'] = $arResult['PAGE_NUMBER'] == 0 ? 1 : $arResult['PAGE_NUMBER'];


// Находим общее число страниц
$arResult['TOTAL'] = intval(($arResult['COUNT'] - 1) / $arResult['PAGE_COUNT']) + 1;
$arResult['TOTAL'] = ceil($arResult['COUNT']/$arResult['PAGE_COUNT']);

!$arResult['PAGE_NUMBER'] || $arResult['PAGE_NUMBER'] > $arResult['TOTAL'] ? $arResult['PAGE_NUMBER'] = 1 : '';

// Находим номер предыдущей страницы
$arResult['PAGE_NUMBER'] > 1 ? $arResult['PREV_NUMBER'] = $arResult['PAGE_NUMBER']-1 : '';

// Находим номер следующей страницы
$arResult['PAGE_NUMBER'] > 0 && $arResult['PAGE_NUMBER'] < $arResult['TOTAL'] ? $arResult['NEXT_NUMBER'] = $arResult['PAGE_NUMBER']+1 : '';

// Определяем номер страницы с которой начинаем и которой заканчиваем выводить навигацию
$LEFT_COEF = $arParams['PAGE_NUMBER']-$arResult['SHOW_COUNT']/2;
$RIGHT_COEF = $arParams['PAGE_NUMBER']+$arResult['SHOW_COUNT']/2;

//var_dump($LEFT_COEF);
//var_dump($RIGHT_COEF);

if($LEFT_COEF <= 0)
{
	$RIGHT_COEF -= $LEFT_COEF -1;
	$LEFT_COEF = 1;
}
elseif($RIGHT_COEF >= $arResult['TOTAL'])
{
	$LEFT_COEF =  $LEFT_COEF + $arResult['TOTAL'] - $RIGHT_COEF;
	$RIGHT_COEF = $arResult['TOTAL'];
} 

//var_dump($LEFT_COEF);
//var_dump($RIGHT_COEF);


$arResult['START_SHOW_NUMBER'] = $LEFT_COEF;
$arResult['END_SHOW_NUMBER'] = $RIGHT_COEF;


$arResult['eitherside'] = ($arResult['SHOW_COUNT'] * $arResult['PAGE_COUNT']);


$this->IncludeTemplate();